package LearnDataStructure.d_图结构.a_深度优先搜索DFS;

import java.util.Scanner;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-14 19:34
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.d_图结构.例题
 */
/*
 * 输入一个m行n列的字符矩阵，统计字符“@”组成多少个八连块。
 * 如果两个字符“@”所在的格子相邻（横、竖或者对角线方向），就说它们属于同一个八连块。
5
*@@*@
**@*@
****@
@@@*@
@@**@

3

 */
public class b_八连通FloodFillbyDFSand回溯 {
    public static void main(String[] args) {
        b_八连通FloodFillbyDFSand回溯 test = new b_八连通FloodFillbyDFSand回溯();
        test.useCheck();
    }

    public void useCheck() {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        char[][] arr = new char[N][N];
        int count = 0;
        for (int i = 0; i < N; i++) {
            arr[i] = scanner.next().toCharArray();
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (arr[i][j] == '@') {
                    check(arr,i,j);
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    public void check(char[][] arr, int row, int col) {
        arr[row][col] = '*';
        for (int x = -1; x < 2; x++) {
            for (int y = -1; y < 2; y++) {
                if (x == 0 && y == 0) {
                    continue;
                }
                if (row + x >= 0 && row + x < arr.length && col + y >= 0 && col + y < arr.length) {
                    if (arr[row + x][col + y] == '@') {
                        check(arr,row + x,col + y);
                    }
                }
            }
        }
    }
}
